package com.example.wupic.bestball;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    ViewPager viewPager=null;//定义一个控件变量
    ImageView imageView1=null;
    ImageView imageView2=null;
    List<Fragment> fragmentList=new ArrayList<Fragment>();//fragment集合
    FragmentPagerAdapter fragmentPagerAdapter=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager=findViewById(R.id.viewpager);//获取控件
       imageView1=   findViewById(R.id.btn_home);
       imageView2=   findViewById(R.id.btn_person);
        fragmentList.add(new HomeFragment());
        fragmentList.add(new PersonFragment());
        //匿名内部类
        fragmentPagerAdapter=new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return fragmentList.get(i);
            }
            @Override
            public int getCount() {
                return fragmentList.size();
            }
        };
        viewPager.setAdapter(fragmentPagerAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {//页面切换方法
               viewPager.setCurrentItem(i);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        addEvent();
    }

    public  void selectTab(int i){
        resetImgs();
             switch (i){
                 case 0:
                     imageView1.setImageResource(R.drawable.icon_home);
                     break;
                 case 1:
                     imageView2.setImageResource(R.drawable.icon_person);
                     break;
             }
             viewPager.setCurrentItem(i);

    }
    @Override
    public void onClick(View v) {
        resetImgs();
               switch (v.getId()){
                   case R.id.btn_home:
                       //设置选中状态
                       selectTab(0);
                       break;
                   case  R.id.btn_person:
                       //设置选中状态
                       selectTab(1);
                       break;
               }
    }
    public void addEvent(){
        imageView1.setOnClickListener(this);
        imageView2.setOnClickListener(this);
    }
    public void resetImgs(){
        imageView1.setImageResource(R.drawable.icon_home1);
        imageView2.setImageResource(R.drawable.icon_person1);

    }

}
